package com.aysafety.iotdataplatformweb.controller.microseism;

import com.aysafety.core.common.CheckUtils;
import com.aysafety.core.common.Common;
import com.aysafety.core.common.DataConversionUtil;
import com.aysafety.core.common.DateUtils;
import com.aysafety.iotdataplatformweb.controller.base.BaseController;
import com.aysafety.iotdataplatformweb.services.microseism.MicroseismService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/mineMonitor/realMonitor/microseismMonitor")
public class MicroseismController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MicroseismService microseismService;

    @RequestMapping("/microseismRealTime")
    public Map<String, Object> microseismRealTime(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            if (!params.containsKey("mineId")) {
                return this.formatReturnData("缺少参数");
            }
            String mineId = params.get("mineId").toString();
            String startTime = params.containsKey("startTime") ? params.get("startTime").toString()
                : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            startTime =
                StringUtils.isNotBlank(startTime) ? startTime : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            String endTime = params.containsKey("endTime") ? params.get("endTime").toString()
                : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            endTime = StringUtils.isNotBlank(endTime) ? endTime : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String timeSpanStr = params.containsKey("timeSpan") ? params.get("timeSpan").toString() : "0";
            timeSpanStr = StringUtils.isNotBlank(timeSpanStr) ? timeSpanStr : "0";
            int timeSpan = Integer.parseInt(timeSpanStr);
            String timeUnit = params.containsKey("timeUnit") ? params.get("timeUnit").toString() : "s";
            timeUnit = StringUtils.isNotBlank(timeUnit) ? timeUnit : "s";
            String pageStr = params.containsKey("page") ? params.get("page").toString() : "0";
            pageStr = StringUtils.isNotBlank(pageStr) ? pageStr : "0";
            int page = Integer.parseInt(pageStr);
            String sizeStr = params.containsKey("size") ? params.get("size").toString() : "10000";
            sizeStr = StringUtils.isNotBlank(sizeStr) ? sizeStr : "10000";
            int size = Integer.parseInt(sizeStr);
            String startRow = params.containsKey("startRow") ? params.get("startRow").toString() : "";
            String firstStartRow = params.containsKey("firstStartRow") ? params.get("firstStartRow").toString() : "";
            data.put("total", microseismService.countByMicroseism(mineId, startTime, endTime, timeSpan, timeUnit));
            result = Common.getData(microseismService.queryRealTimeByMicroseism(mineId, startTime, endTime, timeSpan,
                timeUnit, page, size + 1, startRow), size, startRow, firstStartRow);
            data.put("list", result.get("list"));
            data.put("firstStartRow", result.get("firstStartRow"));
            data.put("lastStartRow", result.get("lastStartRow"));
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("MicroseismController-microseismRealTime:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("MicroseismController-microseismRealTime:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    /**
     * @Description: 大能量微震事件
     * @Param: [params, request, response]
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @Author: ChengQi
     * @Date: 2018/12/23
     */
    @RequestMapping("/largeEnergy")
    public Map<String, Object> largeEnergy(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);
            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);
            // 矿区id、工作面必传
            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            List<Map<String, Object>> results = microseismService.queryWeblargeEnergy(mineId, startTime, endTime);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("MicroseismController-microseismRealTime:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("MicroseismController-microseismRealTime:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    /**
     * @Description: 频次
     * @Param: [params, request, response]
     * @return: java.util.Map<java.lang.String , java.lang.Object>
     * @Author: ChengQi
     * @Date: 2018/12/22
     */
    @RequestMapping("/frequency")
    public Map<String, Object> frequency(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);
            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);
            // 矿区id、工作面必传
            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            startTime = DateUtils.getBeforeDayDate(-30,startTime,"yyyy-MM-dd HH:mm:ss");

            String sizeStr = params.containsKey("size") ? params.get("size").toString() : "10000";
            sizeStr = StringUtils.isNotBlank(sizeStr) ? sizeStr : "10000";
            //int size = Integer.parseInt(sizeStr);
            int size = 10000;

            data.put("total", microseismService.countWebFrequency(mineId, startTime, endTime));
            List<Map<String, Object>> results = microseismService.queryWebFrequency(mineId, startTime, endTime,size);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("MicroseismController-largeEnergy:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("MicroseismController-largeEnergy:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }


    /**
    * @Description: 能量偏差值
    * @Param: [params, request, response] 
    * @return: java.util.Map<java.lang.String,java.lang.Object> 
    * @Author: ChengQi 
    * @Date: 2019/3/8 
    */ 
    @RequestMapping("/energyDeviation")
    public Map<String, Object> energyDeviation(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);
            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);
            // 矿区id、工作面必传
            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            List<Map<String, Object>> results = microseismService.queryWebEnergyDeviation(mineId, startTime, endTime);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("MicroseismController-microseismRealTime:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("MicroseismController-microseismRealTime:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    /** 
    * @Description:离散度
    * @Param: [params, request, response] 
    * @return: java.util.Map<java.lang.String,java.lang.Object> 
    * @Author: ChengQi 
    * @Date: 2019/3/13 
    */ 
    @RequestMapping("/queryDispersion")
    public Map<String, Object> queryDispersion(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);
            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);
            // 矿区id、工作面必传
            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            List<Map<String, Object>> results = microseismService.queryDispersion(mineId, startTime, endTime);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("MicroseismController-microseismRealTime:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("MicroseismController-microseismRealTime:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    /**
    * @Description: 频次偏差
    * @Param: [params, request, response] 
    * @return: java.util.Map<java.lang.String,java.lang.Object> 
    * @Author: ChengQi 
    * @Date: 2019/3/8 
    */ 
    @RequestMapping("/frequencyDeviation")
    public Map<String, Object> frequencyDeviation(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);
            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);
            // 矿区id、工作面必传
            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            String sizeStr = params.containsKey("size") ? params.get("size").toString() : "10000";
            sizeStr = StringUtils.isNotBlank(sizeStr) ? sizeStr : "10000";
            //int size = Integer.parseInt(sizeStr);
            int size = 10000;

            data.put("total", microseismService.countWebFrequency(mineId, startTime, endTime));
            List<Map<String, Object>> results = microseismService.queryWebFrequency(mineId, startTime, endTime,size);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("MicroseismController-microseismRealTime:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("MicroseismController-microseismRealTime:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    /**
    * @Description: 频次比
    * @Param: [params, request, response] 
    * @return: java.util.Map<java.lang.String,java.lang.Object> 
    * @Author: ChengQi 
    * @Date: 2019/3/8 
    */ 
    @RequestMapping("/frequencyPer")
    public Map<String, Object> frequencyPer(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();
            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);
            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);
            // 矿区id、工作面必传
            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            List<Map<String, Object>> results = microseismService.frequencyPer(mineId, startTime, endTime);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("MicroseismController-frequencyPer:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("MicroseismController-frequencyPer:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    /**
     * @Description: 综合预警
     * @Param: [params, request, response]
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @Author: ChengQi
     * @Date: 2019/1/3
     */
    @RequestMapping("/comprehensiveWarning")
    public Map<String, Object> comprehensiveWarning(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> data = new HashMap<String, Object>();

            // 矿区id
            String mineId = DataConversionUtil.getStrFromMap("mineId", params);

            // 开始时间
            String startTime = DataConversionUtil.getStrFromMap("startTime", params);

            if (StringUtils.isBlank(mineId) || StringUtils.isBlank(startTime)) {
                return this.formatReturnData("缺少参数");
            }
            if (!CheckUtils.isValidDate(startTime, "yyyy-MM-dd HH:mm:ss")) {
                return this.formatReturnData("日期格式不正确");
            }
            // 结束时间
            String defaultEndTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String endTime = DataConversionUtil.getStrFromMapOrSetDefaultValue("endTime", defaultEndTime, params);

            Map<String, Object> results = microseismService.comprehensiveWarning(mineId, startTime, endTime);
            data.put("list", results);
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("MicroseismController-microseismRealTime:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("MicroseismController-microseismRealTime:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }


}
